package com.sense.kukoo.biz_explore.adapter.provider

import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.common.R
import com.sense.kukoo.biz_explore.adapter.SearchAdapter
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.bean.SearchBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class SearchResultContentItemProvider(val listener: SearchAdapter.SearchEventListener):
    MultiItemProvider<SearchBean>() {

    val dp8:Int = ApplicationContext.dp2px(8F)
    var mCurrentMusic: LocalMediaListItemBean? = null
    var mCurrentPlayState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING

    override fun getItemType(): Int = SearchBean.TYPE_SEARCH_RESULT_CONTENT

    override fun getItemLayoutId(): Int = R.layout.common_item_channel_content_normal

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: SearchBean) {
        val data: IndexContentBean = bean.content!!
        //
        viewHolder.getView(R.id.cl_item_root).setOnClickListener {
            listener.onSearchResultContentItemClick(bean)
        }
        //
        val iv_icon_channel = viewHolder.getView(R.id.iv_icon_channel) as ImageView
        Monet.get(iv_icon_channel.context)
            .load<GlideConfig>(data.avatarUrl)
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_icon_channel)
        //
        val tv_name_channel = viewHolder.getView(R.id.tv_name_channel) as TextView
        tv_name_channel.text = data.channelName
        //
        viewHolder.getView(R.id.click_channel).setOnClickListener {
            listener.onSearchResultChannelClick(bean)
        }
        //
        val tv_title_item = viewHolder.getView(R.id.tv_title_item) as TextView
        tv_title_item.text = data.title
        //
        val iv_icon_item = viewHolder.getView(R.id.iv_icon_item) as ImageView
        Monet.get(iv_icon_item.context)
            .load<GlideConfig>(data.coverUrl)
            .centerCropWithConner(dp8)
            .into(iv_icon_item)
        //
        val tv_time_item = viewHolder.getView(R.id.tv_time_item) as TextView
        var min = (data.duration ?: 0) / 60
        min = if(min == 0) 1 else min
        val hour = min / 60
        min = min % 60
        if(hour > 0){
            tv_time_item.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.time_formate_min_and_hour, hour, min)
        } else {
            tv_time_item.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.time_formate_min, min)
        }
        //
        val tv_listens_item = viewHolder.getView(R.id.tv_listens_item) as TextView
        tv_listens_item.text = "${data.listenNum ?: 0}"
        //
        val tv_likes_item = viewHolder.getView(R.id.tv_likes_item) as TextView
        tv_likes_item.text = "${data.likeNum ?: 0}"
        //
        val iv_icon_info = viewHolder.getView(R.id.iv_icon_play) as ImageView
        if(data.id == (mCurrentMusic?.idOfChapter() ?: 0)){
            //当前播放的音乐
            when (mCurrentPlayState) {
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING ->{
                    iv_icon_info.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_card_pause_36dp)
                }
                /*UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING,
                UserWatcher.MusicStateWatcher.STATE_PAUSE ->{ }*/
                else ->{
                    iv_icon_info.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_card_start_36dp)
                }
            }
        } else {
            iv_icon_info.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_card_start_36dp)
        }
        //
        val click_icon_info = viewHolder.getView(R.id.click_icon_play)
        click_icon_info.setOnClickListener {
            listener.onSearchResultPlayClick(bean)
        }
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int){
        mCurrentPlayState = state
    }
}